package com.bloveoacom.weichi.adapter;

import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.SpannableStringBuilder;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SpanUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bloveoacom.base.UserInfoBean;
import com.bloveoacom.util.ActivityUtil;
import com.bloveoacom.util.GlideUtil;
import com.bloveoacom.util.MyClickableSpan;
import com.bloveoacom.util.StringUtils;
import com.bloveoacom.weichi.R;
import com.bloveoacom.weichi.activity.AllCommentActivity;
import com.bloveoacom.weichi.bean.CommentBean;
import com.bloveoacom.weichi.bean.PostDetailBean;
import com.bloveoacom.weichi.bean.CommentHelperModel;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * author: yjl
 * e-mail: 1271901673@qq.com
 * time  : 2018/9/20
 * desc  :
 */
public class MyCommentAdapter extends BaseQuickAdapter<CommentBean,BaseViewHolder>{
    public MyCommentAdapter(int layoutResId, @Nullable List<CommentBean> data) {
        super(layoutResId, data);
    }

    PostDetailBean postDetailBean;
    MyClickableSpan.MyClickable myClickable;
    public void setPDB(PostDetailBean postDetailBean){
        this.postDetailBean  = postDetailBean;
    }

    public void setMyClicker(MyClickableSpan.MyClickable myClickable){
        this.myClickable  = myClickable;
    }

    @Override
    protected void convert(BaseViewHolder helper, CommentBean item) {
        GlideUtil.loadCircleImage((ImageView) helper.getView(R.id.iv_head), StringUtils.getFullUrl(item.getHeadImg(), 0));
        helper.setText(R.id.tv_username, item.userName);
        helper.setText(R.id.tv_time, item.getCreateTime());
        helper.setText(R.id.tv_content, item.getComment());
        boolean isShowLZ2 = false;
        if(postDetailBean!=null){
            isShowLZ2 = String.valueOf(item.getUserId()).equals(postDetailBean.getUserId());
        }
        helper.setVisible(R.id.tv_is_owner,isShowLZ2);
        helper.setText(R.id.tv_replay_comment,"");
        if(StringUtils.isEmpty(item.replyCommentId)){
            helper.setVisible(R.id.tv_replay_comment,false);
        }else{
            helper.setVisible(R.id.tv_replay_comment,true);
            helper.setText(R.id.tv_replay_comment,new SpanUtils().appendLine(item.replyUserName).setForegroundColor(ContextCompat.getColor(mContext,R.color.black)).append(item.replyComment).create());
        }
        //helper.addOnClickListener(R.id.tv_replay_comment);
        //LinearLayout llcoment =  helper.getView(R.id.ll_comment);
//        llcoment.removeAllViews();
//        List<SpannableStringBuilder> spannableStringBuilders = setCommentSonData(item);
//        helper.setVisible(R.id.ll_comment,item.allCommentNum>0);
//        for (int i = 0; i < spannableStringBuilders.size(); i++) {
//            TextView textView = new TextView(mContext);
//            textView.setText(spannableStringBuilders.get(i));
//            MyClickableSpan.setClickeAndBgColor(mContext,textView);
//            llcoment.addView(textView);
//        }
//        if(item.allCommentNum>2){
//            TextView textView = new TextView(mContext);
//            textView.setText(getCommentMore(item));
//            MyClickableSpan.setClickeAndBgColor(mContext,textView);
//            llcoment.addView(textView);
//        }
    }


    CommentBean commentBean;
    private SpannableStringBuilder getCommentMore(CommentBean commentBean2){
        SpanUtils spanUtils = new SpanUtils();
        spanUtils.append("查看全部"+commentBean2.allCommentNum+"条评论").setForegroundColor(ContextCompat.getColor(mContext,R.color.colorPrimary)).setClickSpan(new MyClickableSpan(commentBean2, new MyClickableSpan.MyClickable() {
            @Override
            public void myClickable(Object map) {
                commentBean = (CommentBean) map;
                ActivityUtil.startActivity(mContext,AllCommentActivity.class,String.valueOf(commentBean.getCommentId()),String.valueOf(commentBean.allCommentNum));

            }
        })).appendImage(R.drawable.ic_comment_more, SpanUtils.ALIGN_CENTER);
        return spanUtils.create();
    }


    private List<SpannableStringBuilder> setCommentSonData(CommentBean commentBean2){


        //第一个名字 看是不是楼主
        //看回复的的人是不是层主
        List<SpannableStringBuilder> sbs = new ArrayList<>();
        if(commentBean2.commentSonList==null){
                return sbs;
        }
        for (CommentBean.CommentSon commentSon : commentBean2.commentSonList) {
            SpanUtils spanUtils = new SpanUtils();
            //判断第一个名字是不是楼主
            boolean isShowLZ = String.valueOf(commentSon.userId).equals(postDetailBean.getUserId());
            //判断第二个名字是不是楼主
            boolean isShowLZ2 = String.valueOf(commentSon.replyUserId).equals(postDetailBean.getUserId());

            //判断第二个名字是不是层主
            boolean noHasReply = String.valueOf(commentSon.replyUserId).equals(String.valueOf(commentBean2.getUserId()))||String.valueOf(commentSon.replyUserId).equals(String.valueOf(commentSon.userId));

            if (!isShowLZ && !isShowLZ2) {
                if (noHasReply) {
                    //是层主没有回复按钮
                    spanUtils.append(commentSon.userName + ":").setForegroundColor(ContextCompat.getColor(mContext, R.color.colorPrimary)).setClickSpan(
                            new MyClickableSpan(new CommentHelperModel(commentBean2, commentSon.userId,commentSon.userName), myClickable));
                } else {
                    //有回复按钮
                    spanUtils.append(commentSon.userName).setForegroundColor(ContextCompat.getColor(mContext, R.color.colorPrimary)).setClickSpan(
                            new MyClickableSpan(new CommentHelperModel(commentBean2, commentSon.userId,commentSon.userName), myClickable)).append("回复").append(commentSon.replyUserName + ":").setForegroundColor(ContextCompat.getColor(mContext, R.color.colorPrimary))
                            .setClickSpan(new MyClickableSpan(new CommentHelperModel(commentBean2, commentSon.replyUserId,commentSon.replyUserName), myClickable));
                }
            } else if (isShowLZ) {
                //回复前面的名字是楼主
                if (noHasReply) {
                    //是层主没有回复按钮
                    spanUtils.append(commentSon.userName).setForegroundColor(ContextCompat.getColor(mContext, R.color.colorPrimary)).setClickSpan(
                            new MyClickableSpan(new CommentHelperModel(commentBean2, commentSon.userId,commentSon.userName),myClickable)).appendImage(R.drawable.ic_owner, SpanUtils.ALIGN_CENTER).append(":").setForegroundColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
                } else {
                    //有回复按钮
                    spanUtils.append(commentSon.userName).setForegroundColor(ContextCompat.getColor(mContext, R.color.colorPrimary)).setClickSpan(new MyClickableSpan(new CommentHelperModel(commentBean2, commentSon.userId,commentSon.userName), myClickable))
                            .appendImage(R.drawable.ic_owner, SpanUtils.ALIGN_CENTER)
                            .append("回复").append(commentSon.replyUserName + ":").setForegroundColor(ContextCompat.getColor(mContext, R.color.colorPrimary)).setClickSpan(
                            new MyClickableSpan(new CommentHelperModel(commentBean2, commentSon.replyUserId,commentSon.replyUserName), myClickable));
                }

            } else if (isShowLZ2) {
                //回复后面的名字是楼主
                if (noHasReply) {
                    //是层主没有回复按钮
                    spanUtils.append(commentSon.userName+":").setForegroundColor(ContextCompat.getColor(mContext, R.color.colorPrimary)).setClickSpan(
                            new MyClickableSpan(new CommentHelperModel(commentBean2, commentSon.userId,commentSon.userName), myClickable));
                } else {
                    //有回复按钮
                    spanUtils.append(commentSon.userName).setForegroundColor(ContextCompat.getColor(mContext, R.color.colorPrimary)).setClickSpan(
                            new MyClickableSpan(new CommentHelperModel(commentBean2, commentSon.userId,commentSon.userName), myClickable)).append("回复").append(commentSon.replyUserName).setForegroundColor(ContextCompat.getColor(mContext, R.color.colorPrimary)).setClickSpan(
                            new MyClickableSpan(new CommentHelperModel(commentBean2, commentSon.replyUserId,commentSon.replyUserName), myClickable)).append(":").setForegroundColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
                }

            }
            sbs.add(spanUtils.append(commentSon.comment).create());

        }
        return  sbs;
    }


}
